package com.xc.erp.modules.erp.controller.depot;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xc.erp.common.annotation.Log;
import com.xc.erp.common.utils.SecurityUtils;
import com.xc.erp.modules.erp.domain.dto.DepotParam;
import com.xc.erp.modules.erp.domain.pojo.JshDepot;
import com.xc.erp.modules.erp.domain.pojo.JshDepotHead;
import com.xc.erp.modules.erp.service.JshDepotService;
import com.xc.erp.modules.erp.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/depot")
@Api(tags = {"仓库管理"})
public class DepotController {
    @Autowired
    private JshDepotService jshDepotService;

    @GetMapping("/findDepotByCurrentUser")
    @ApiOperation(value = "获取当前用户拥有权限的仓库列表")
    public AjaxResult list() {
        return new AjaxResult(200, "成功", jshDepotService.depotList());
    }

    //查询仓库name是否唯一
    @GetMapping("/checkIsNameExist")
    public AjaxResult checkIsNameExist(@Param("name") String name) {
        return AjaxResult.success().put("status", jshDepotService.checkIsNameExist(name));
    }

    @ApiOperation("查询所有的仓库信息")
    @GetMapping("list/{page}/{limit}")
    public AjaxResult listDepot(@PathVariable Integer page, @PathVariable Integer limit, DepotParam depotParam){
        IPage<JshDepot> iPage = jshDepotService.listPage(page, limit, depotParam);
        return AjaxResult.success(iPage);
    }

    /**
     * 新增
     * @param jshDepot
     * @return
     */
    @PostMapping("/add")
    @Log(modul = "仓库管理",desc = "添加仓库")
    public AjaxResult add(@RequestBody JshDepot jshDepot) {
        jshDepot.setTenantId(SecurityUtils.getLoginUser().getJshUser().getTenantId());
        if (jshDepotService.add(jshDepot)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    @ApiOperation("仓库修改")
    @PutMapping("update")
    @Log(modul = "仓库管理",desc = "修改仓库信息")
    public AjaxResult update(@RequestBody JshDepot jshDept){
        if(jshDepotService.updateById(jshDept)){
            return AjaxResult.success();
        } else {
            return AjaxResult.error("修改异常");
        }
    }

    @Log(modul = "仓库管理",desc = "批量修改仓库状态")
    @PutMapping("/setStatus/{status}")
    public AjaxResult update(@RequestBody String[] ids,@PathVariable boolean status){
        if (jshDepotService.updateStatus(ids,status)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @Log(modul = "仓库管理",desc = "修改仓库默认状态")
    @ApiOperation("设为默认")
    @PutMapping("defaultDepot/{id}")
    public AjaxResult defaultDepot(@PathVariable long id,boolean isDefault){
        if(jshDepotService.defaultUpdateDepot(id,isDefault)){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    @Log(modul = "仓库管理",desc = "删除仓库信息")
    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestBody String[] ids){
        if (jshDepotService.delete(ids)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @Log(modul = "仓库管理",desc = "批量删除仓库信息")
    @DeleteMapping("/del/{id}")
    public AjaxResult remove(@PathVariable long id){
        if(jshDepotService.removeById(id)){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }
}